package com.example.dishit.model.dbproxy.data;

import org.json.JSONException;
import org.json.JSONObject;

public class Dish {
	Review review;
	String disher;
	String imageURL;

	public String getDisher() {
		return disher;
	}

	public void setDisher(String disher) {
		this.disher = disher;
	}

	public Review getReview() {
		return review;
	}

	public void setReview(Review review) {
		this.review = review;
	}

	public String getImageURL() {
		return imageURL;
	}

	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}

	public Dish(Review review, String disher) {
		setReview(review);
		setDisher(disher);
		setImageURL("");
	}

	public String getJSON() {
		String dishString;
		StringBuffer dishBuffer = new StringBuffer();
		dishBuffer.append("{\"dish\":{\"username\":\"" + this.disher
				+ "\",\"imageURL\":\"" + this.getImageURL()
				+ "\",\"review\":{\"comments\":\"" + this.review.getComments()
				+ "\",\"rating\":\"" + Float.toString(this.review.getRating())
				+ "\",\"restaurant\":{\"name\":\""
				+ this.getReview().getRestaurant().getName()
				+ "\",\"street\":\""
				+ this.getReview().getRestaurant().getStreet()
				+ "\",\"city\":\"" + this.getReview().getRestaurant().getCity()
				+ "\",\"state\":\""
				+ this.getReview().getRestaurant().getState()
				+ "\",\"zipcode\":"
				+ this.getReview().getRestaurant().getZipCode() + "}}}}");
		dishString = dishBuffer.toString();
		return dishString;
	}

	public static Dish buildFromJSON(JSONObject json) throws JSONException {
		JSONObject dishData = new JSONObject(json.getString("dish"));
		JSONObject reviewData = new JSONObject(dishData.getString("review"));
		JSONObject restaurantData = new JSONObject( 
				reviewData.getString("restaurant"));
		Dish returnDish = new Dish(new Review(new Restaurant(
				restaurantData.getString("name"),
				restaurantData.getString("street"),
				restaurantData.getString("city"),
				restaurantData.getString("state"),
				Integer.parseInt(restaurantData.getString("zipcode"))),
				reviewData.getString("comments"), Float.parseFloat(reviewData
						.getString("rating"))), dishData.getString("username"));
		returnDish.setImageURL(dishData.getString("imageURL").replace("\\", "/"));
		return returnDish;
	}
}
